package com.ga01.controller;

import com.ga01.entity.Clazz;
import com.ga01.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes")
//@RequestMapping("/test")
public class ClazzController {

    @GetMapping("/hello")
    public String test() {
        return "Hello World"; // 简单返回字符串
    }

    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public List<Clazz> getAllClasses() {
        return clazzService.list();
    }

    @GetMapping("/{id}") // 路径：GET /classes/{id}
    public Clazz getClassById(@PathVariable Integer id) { // @PathVariable 获取路径中的 ID
        return clazzService.getById(id); // 调用 MyBatis-Plus 的 getById 方法
    }

    @PostMapping
    public boolean addClass(@RequestBody Clazz clazz) {
        return clazzService.save(clazz);
    }

    @PutMapping("/{id}")
    public boolean updateClass(@PathVariable Integer id, @RequestBody Clazz clazz) {
        clazz.setId(id);
        return clazzService.updateById(clazz);
    }

    @DeleteMapping("/{id}")
    public boolean deleteClass(@PathVariable Integer id) {
        return clazzService.removeById(id);
    }
}